﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Assessment.Management.SharedComponent.Entities;
using Assessment.Management.SharedComponent.Constants;
using Assessment.Management.SharedComponent.Params.ManagerParam;
using Assessment.Management.BIZ;
using System.Web.UI.HtmlControls;

namespace Assessment.Management.UI.Admin.UserControls.Classes
{
    public partial class ClassManager : System.Web.UI.UserControl
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                DepartmentParam param = new DepartmentParam(FunctionType.GetListDepartment);
                MainController.Provider.Execute(param);
                ddlKhoa.DataSource = param.listDepartment;
                ddlKhoa.DataBind();
                ddlKhoa.Items.Insert(0, new ListItem("----------Chọn Khoa----------", ""));
                ddlKhoa.SelectedIndex = 0;
                LoadGridView();
            }
        }
        protected void btnExit_Click(object sender, EventArgs e)
        {
            Response.Redirect("Dashboard.aspx");
        }
        protected void ddlKhoa_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlKhoa.SelectedIndex.ToString() == "0")
            {
                LoadGridView();
            }
            else
            {
                ClassParam param = new ClassParam(FunctionType.GetListClassByDepartmentID);
                param.Class = new Class() { DepartmentID = Convert.ToInt32(ddlKhoa.SelectedValue) };
                MainController.Provider.Execute(param);
                grvDSL.DataSource = param.listClass;
                grvDSL.DataBind();
            }

        }

        protected void chkSelectAll_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox chkAll = (CheckBox)grvDSL.HeaderRow.FindControl("chkSelectAll");
            foreach (GridViewRow gvRow in grvDSL.Rows)
            {
                CheckBox chkSel = (CheckBox)gvRow.FindControl("chkSelect");
                chkSel.Checked = chkAll.Checked;
            }
        }



        protected void grvDSL_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Class cl = (Class)e.Row.DataItem;
                HiddenField hidSubjectID = (HiddenField)e.Row.FindControl("hidClassID");
                hidSubjectID.Value = cl.ID.ToString();
                HiddenField hidVersion = (HiddenField)e.Row.FindControl("hidVersion");
                hidVersion.Value = cl.Version.ToString();

                HyperLink hypEdit = (HyperLink)e.Row.FindControl("hypEdit");
                hypEdit.NavigateUrl = string.Format(SMX.smx_ClassEdit, cl.ID);

                ImageButton btnDelete = (ImageButton)e.Row.FindControl("btnDelete");
                string msg = string.Format("Bạn có chắc chắn muốn xóa môn học có mã \"{0}\"?", cl.ID);
                btnDelete.CommandName = "Delete";
                btnDelete.OnClientClick = string.Format("return confirm('{0}');", msg);
            }
        }
        //Sự kiện xóa dòng
        protected void grvDSL_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            GridViewRow row = grvDSL.Rows[e.RowIndex];
            HiddenField hidProductID = (HiddenField)row.FindControl("hidClassID");
            HiddenField hidVersion = (HiddenField)row.FindControl("hidVersion");
            List<Class> lstcl = new List<Class>();
            lstcl.Add(new Class()

            {
                ID = int.Parse(hidProductID.Value.ToString()),
                Version = int.Parse(hidVersion.Value),
                Deleted = SMX.smx_IsDeleted,
                UpdatedBy = SMX.smx_UpdatedBy,
                UpdatedDTG = DateTime.Now
            });
            DeleteClass(lstcl);
            LoadGridView();
        }
        //Sự kiện chuyển trang
        protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            LoadGridView();
            grvDSL.PageIndex = e.NewPageIndex;
            grvDSL.DataBind();
        }
        //#endregion
        //#region methods

        //Lấy dữ liệu đổ lên gridview
        private void LoadGridView()
        {
            ClassParam param = new ClassParam(FunctionType.GetClasss);
            MainController.Provider.Execute(param);
            grvDSL.DataSource = param.listClass;
            grvDSL.DataBind();
            hypAddNew.HRef = SMX.smx_ClassAdd;
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                List<Class> lstcl = GetSelectedItemOnGrid();
                DeleteClass(lstcl);
                //Load lại gridview
                LoadGridView();
            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.Message;
            }
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            lblMessage.Text = string.Empty;
            SearchClasses();

        }

        //Lấy dữ liệu trên Gridview
        private Class GetItemOnGrid(GridViewRow item)
        {
            Class result = new Class();
            HiddenField hidId = (HiddenField)item.FindControl("hidClassID");
            HiddenField hidVersion = (HiddenField)item.FindControl("hidVersion");
            result.ID = int.Parse(hidId.Value);
            result.Version = int.Parse(hidVersion.Value);
            result.Deleted = SMX.smx_IsDeleted;
            result.UpdatedBy = SMX.smx_UpdatedBy;
            result.UpdatedDTG = DateTime.Now;
            return result;
        }

        //Lây dữ liệu các dòng được check trên Gridview
        private List<Class> GetSelectedItemOnGrid()
        {
            List<Class> list = new List<Class>();

            foreach (GridViewRow gvRow in grvDSL.Rows)
            {
                CheckBox ckSelected = (CheckBox)gvRow.FindControl("chkSelect");
                if (ckSelected.Checked)
                {
                    list.Add(GetItemOnGrid(gvRow));
                }
            }
            return list;
        }

        private void DeleteClass(List<Class> lstcl)
        {
            ClassParam param = new ClassParam(FunctionType.DeleteClasses);
            param.listClass = lstcl;
            MainController.Provider.Execute(param);
        }

        private void SearchClasses()
        {
            ClassParam param = new ClassParam(FunctionType.SearchClasses);
            Class cl = new Class();
            //subject.ID = int.Parse(txtMaMH.Text);
            cl.ClassName = txtTenL.Text.Trim();
            param.Class = cl;
            MainController.Provider.Execute(param);
            grvDSL.DataSource = param.listClass;
            grvDSL.DataBind();

        }
    }
}